package com.sanjiahantang.youjiarole.module.me.address.views.impl;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.sanjiahantang.plugin.pickerview.OptionsPickerView;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.module.me.address.beans.AddressDetailBean;
import com.sanjiahantang.youjiarole.module.me.address.presenters.AddressPresenter;
import com.sanjiahantang.youjiarole.module.me.address.views.AddressView;
import com.sanjiahantang.youjiarole.utils.CheckUtil;
import com.sanjiahantang.youjiarole.utils.KeyBoardUtils;

import java.util.ArrayList;


/**
 * Created by zy on 2017/9/22.
 * 编辑/修改 收货地址
 */
public class AddressActivity extends BaseMvpActivity<AddressView,AddressPresenter> implements View.OnClickListener,AddressView {


    private int add_id = 0;
    private EditText editName,editPhone,editStreet;
    private TextView txtArea;
    private View btnAdd;
    private TextView txtRight;

    private String consignee,number,area,street;
    private CheckBox checkBox;
    private OptionsPickerView pvOptions;
    private boolean pickerDataFlag = false;
    private ArrayList<String> listProvince;
    private ArrayList<ArrayList<String>> listCity;
    private ArrayList<ArrayList<ArrayList<String>>> listArea;
    private int type;
    @Override
    public AddressPresenter initPresenter() {
        return new AddressPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_add_address);

        add_id = getIntent().getIntExtra("address",0);
        type = getIntent().getIntExtra(KEY_TYPE,1);

        TextView title = (TextView)findViewById(R.id.title);
        findViewById(R.id.fl_Left).setOnClickListener(this);

        editName = (EditText) findViewById(R.id.editName);
        editPhone = (EditText) findViewById(R.id.editPhone);
        txtArea = (TextView) findViewById(R.id.txtArea);
        editStreet = (EditText) findViewById(R.id.editStreet);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        findViewById(R.id.panelIsDefault).setOnClickListener(this);

        if (type == 0){
            checkBox.setChecked(true);
            checkBox.setEnabled(false);
        }

        checkBox.setOnClickListener(this);

        editName.addTextChangedListener(textWatcher);
        editPhone.addTextChangedListener(textWatcher);
        editStreet.addTextChangedListener(textWatcher);
        txtArea.addTextChangedListener(textWatcher);

        txtRight = (TextView) findViewById(R.id.txtRight);//保存
        btnAdd =  findViewById(R.id.flIcon3);
        btnAdd.setVisibility(View.VISIBLE);

        OnDetailLoadListener listener = new OnDetailLoadListener() {
            @Override
            public void loadData(AddressDetailBean model) {
                if(model.getCode() == RESPONSE_CODE_SUC) {
                    if(!model.getData().equals("")) {
                        editName.setText(model.getData().getCustomerName());
                        editPhone.setText(model.getData().getCellPhone());
                        editStreet.setText(model.getData().getFullAddress());
                        txtArea.setText(model.getData().getAddress());
                        if(1 == model.getData().getIsDefault()){
                            checkBox.setChecked(true);
                        }else {
                            checkBox.setChecked(false);
                        }
                    }else
                        Utils.showToast($this,model.getMessage());
                }else
                    Utils.showToast($this,model.getMessage());
            }

            @Override
            public void setLocate(String tx) {
                txtArea.setText(tx);
            }
        };
        if(0 != add_id) {
            title.setText("编辑收货地址");
            presenter.loadAddress($this,String.valueOf(add_id), listener);
        }else{
            add_id = 0;
            title.setText("新建收货地址");

            editName.setText("");
            editPhone.setText("");
            editStreet.setText("");
            txtArea.setText("");

          //  presenter.setOnDetailLoadListener($this,listener);
        }
        init();
        findViewById(R.id.rlArea).setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            String consignee = editName.getText().toString().trim();
            String number = editPhone.getText().toString().trim();
            area = txtArea.getText().toString().trim();
            street = editStreet.getText().toString().trim();
            if(!consignee.equals("") && !number.equals("") && !area.equals("") &&
                    !street.equals("") && CheckUtil.isMobileNO(number)){
                txtRight.setTextColor(getResources().getColor(R.color.black));
                btnAdd.setClickable(true);
                btnAdd.setOnClickListener(AddressActivity.this);
            } else{
                txtRight.setTextColor(getResources().getColor(R.color.gray_a2));
                btnAdd.setClickable(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                KeyBoardUtils.closeKeybord($this);
                break;
            case R.id.rlArea:
                if (pickerDataFlag) {
                    pvOptions.show();
                }
                break;
            case R.id.flIcon3:
                consignee = editName.getText().toString().trim();
                number = editPhone.getText().toString().trim();
                presenter.saveAddress($this,consignee,street,number,txtArea.getText().toString()
                        ,add_id == 0 ? null : String.valueOf(add_id),checkBox.isChecked()?1:0);
                break;
            case R.id.panelIsDefault:
                checkBox.setChecked(true);
                break;
            case R.id.checkBox:
                checkBox.setChecked(true);
                break;

        }
    }
    public void init() {
        presenter.loadPersonCityList($this, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] listArray = (Object[]) content;
                listProvince = (ArrayList<String>) listArray[0];
                listCity = (ArrayList<ArrayList<String>>) listArray[1];
                listArea = (ArrayList<ArrayList<ArrayList<String>>>) listArray[2];
                pvOptions = new OptionsPickerView.Builder($this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        String tx = listProvince.get(options1) +  " " +
                                listCity.get(options1).get(options2) +  " " +
                                listArea.get(options1).get(options2).get(options3);
                        txtArea.setText(tx);

                    }
                })

                        .setTitleText("")
                        .setDividerColor(Color.BLACK)
                        .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                        .setContentTextSize(20)
                        .build();

        /*pvOptions.setPicker(options1Items);//一级选择器
           pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
                pvOptions.setPicker(listProvince, listCity, listArea);//三级选择器
                pickerDataFlag = true;
            }
        });
    }
    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public void onBackPressed() {
        if(presenter.onBackPressed()){
            return;
        }
        super.onBackPressed();
    }
}
